package com.vcc.msc.sbs.entity;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class WesImageEntity {
	private byte[] image;

	public byte[] getImage() {
		return image;
	}

	public void setImage(byte[] image) {
		this.image = image;
	}

	public static List<WesImageEntity> deserialize(String xml) {
		List<WesImageEntity> imageList = new ArrayList<WesImageEntity>();
		String tag = "";
		int parserEvent = -1;
		if (xml == null) {
			return null;
		}
		try {
			XmlPullParserFactory parserFactory = XmlPullParserFactory
					.newInstance();
			XmlPullParser parser = parserFactory.newPullParser();
			parser.setInput(new StringReader(xml));
			// calls service (referenced in url) to request XML serialized data
			parserEvent = parser.getEventType();

			WesImageEntity image = null;
			while (parserEvent != XmlPullParser.END_DOCUMENT) {
				switch (parserEvent) {
				case XmlPullParser.START_TAG:
					tag = parser.getName();
					if (tag.compareTo("images") == 0) {
						image = new WesImageEntity();
					}
					break;
				case XmlPullParser.END_TAG:
					tag = parser.getName();
					if (tag.compareTo("images") == 0) {
						imageList.add(image);
					}
					break;
				case XmlPullParser.TEXT:
					String text = parser.getText();
					if (text.trim().length() == 0)
						break;

					if (tag.compareTo("image") == 0) {
						image.setImage(text.getBytes());
					}

					break;
				}
				// versionList.add(version);
				parserEvent = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// System.out.println(imageList);
		return imageList;
	}
}
